//
//  CompanyProductListViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/5/5.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#import "CompanyProductListViewController.h"

#import "ProductListTableViewCell.h"

#import "CompanyProductModel.h"

static NSString *const listCell = @"listCell";

@interface CompanyProductListViewController ()<UITableViewDelegate,UITableViewDataSource,TabelViewRefreshDelegate>
@property (nonatomic, strong) RefreshTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CompanyProductListViewController

#pragma mark - Lazy
- (RefreshTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[RefreshTableView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.isRefresh = YES;
        _tableView.refreshDelegate = self;
        _tableView.tableHeaderView = [[UIView alloc]init];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.scrollsToTop = NO;
        
        _tableView.backgroundColor = Bg_Color_FEFDFD;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = Bg_Color_FEFDFD;

    [self.tableView headerRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, App_Frame_Width, CGRectGetHeight(self.view.bounds));
}

#pragma mark - TabelViewRefreshDelegate
- (void)tableView:(UITableView *)tableView Refresh:(BOOL)refresh PageCount:(NSInteger)pageCount {
    [self requestPostBH_Exhibition_getProductByCompanyId:refresh PageCount:pageCount];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
//    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProductListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];
    if (cell == nil) {
        cell = [[ProductListTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:listCell];
    }
    
    cell.editBtn.hidden = YES;
    cell.deleteBtn.hidden = YES;
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 122.0;
}

- (void)_loadData:(id)response Refresh:(BOOL)refresh {
    if (refresh) [self.dataArray removeAllObjects];
    NSArray *arr = [NSArray yy_modelArrayWithClass:[CompanyProductModel class] json:response];
    [self.dataArray addObjectsFromArray:arr];
    if (arr.count < 10) {
        [self.tableView endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}

//根据企业ID获取产品列表
- (void)requestPostBH_Exhibition_getProductByCompanyId:(BOOL)refresh PageCount:(NSInteger)pageCount {
    self.dataArray.count != 0 ?: [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.Id forKey:@"company_id"];
    [params setObject:[NSString stringWithFormat:@"%ld",(long)pageCount] forKey:@"page"];
    [params setObject:@"10" forKey:@"pagesize"];
    
    [PPHTTPRequest requestPostBH_Exhibition_getProductByCompanyId:^(id response, BOOL success) {
        if (self.dataArray.count == 0 && [response isKindOfClass:[NSString class]]) {
            //            self.collectionView.placeholderShow(YES).prompt_title((NSString *)response).isButtonHidden(YES);
        }else self.tableView.placeholderShow(NO);
        if (success && ![response isKindOfClass:[NSString class]]) {
            [self _loadData:response Refresh:refresh];
        }
        if (success && refresh == YES) {
            [self _loadData:response Refresh:refresh];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params];
}
@end
